define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'business/busrecycle/index',
                    del_url: 'business/busrecycle/del',
                    red_url: 'business/busrecycle/reduction',
                    multi_url: 'business/busrecycle/multi',  // 表格的复选框
                    table: 'business',  // 客户表
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), searchable: true },
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'avatar_text', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        {
                            field: 'gender', title: __('Gender'), searchList: { '0': __('Gender 0'), '1': __('Gender 1'), '2': __('Gender 2') },
                            operate: 'FIND_IN_SET',
                            formatter: Table.api.formatter.normal
                        },
                        { field: 'source.name', title: __('Source'), operate: 'LIKE' },
                        { field: 'email', title: __('Email'), operate: 'LIKE' },
                        {
                            field: 'deal', title: __('Deal'), searchList: { '0': __('Deal 0'), '1': __('Deal 1') },
                            operate: 'FIND_IN_SET',
                            formatter: Table.api.formatter.normal
                        },
                        { field: 'admin.nickname', title: __('AdminName'), operate: 'LIKE' },
                        // 日期搜索格式化
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'order', text: '还原', icon: '', confirm: '确定要还原吗', title: '还原',
                                    icon: 'fa fa-magic',
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    url: 'subject/ordrecycle/reduction?ids={id}',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function (err) {
                                        console.log(err);
                                    }
                                },
                            ]
                        }
                    ]
                ],
            });

            // 还原，确认框的方法
            $('.btn-reduction').on('click', function () {
                let ids = Table.api.selectedids(table);
                ids = ids.toString()
                layer.confirm('确定要还原吗?', { title: '还原', btn: ['是', '否'] },
                    function (index) {
                        layer.close(index);
                        $.post($.fn.bootstrapTable.defaults.extend.red_url, { ids: ids, action: 'success', reply: '' }, function (response) {
                            if (response.code == 1) {
                                Toastr.success(response.msg)
                                $(".btn-refresh").trigger('click');
                            } else {
                                Toastr.error(response.msg)
                            }
                        }, 'json')
                    },
                    function (index) {
                        layer.close(index);
                    }
                );

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        del: function () {
            Controller.api.bindevent();
        },
        red: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
